#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/listdir.h"
#include"../include/mount_vfs.h"
#include"../include/create_vfs.h"
#include"../include/vfs_errorcodes.h"
FILE *fp;
void listdirty( char dirname[], int flag, char opfile[] )
{
	//fp = fopen(opfile,"r");
	char *k,temp[MAX_PATH];
	int i=0;
        ntree *temp1,*temp3,*tofind;
	temp3=head;
	llist *list=NULL;
	schflag=0;
	if(!strcmp(dirname,"/"))
	{
		if(mounted==0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTDIR_03);
		else
		{
			fp = fopen(opfile,"w");
			if(fp!=NULL)
			{	 
				trav(temp3,dirname,flag,opfile);
				printf("listdir_SUCCESS\n");
			}
			else
				printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTDIR_04);
		}
	}
	else
	{
		while(dirname[i]!='\0')
		{
			temp[i]=dirname[i];
			i++;
		}
		temp[i]='\0';
		k=strrchr(temp,'/');
		k++;
		if(*k != '\0')
		tofind=search_node(head,dirname);
		else 
		{
			k--;
			*k = '\0';
			tofind=search_node(head,temp);
		}
		schflag=0;
		if(mounted==0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTDIR_03);
		else
		{
			if(tofind != NULL)
				list=find(tofind->file_desc);
			if(list==NULL)
				printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTDIR_01);
			else{
				fp = fopen(opfile,"w");
				if(fp!=NULL)
				{
					trav(temp3,tofind->file_desc->filename,flag,opfile);
					printf("listdir_SUCCESS\n");
				}
				else
					printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTDIR_04);	
			    }
		}
	}
	if(fp!=NULL)
	close(fp);
}

void trav(ntree *head,char dirname1[],int flag,char opfile[])
{
	ntree *temp4,*q1,*temp2;
	
		q1=head;
		if(q1!=NULL)
		{
			temp4=q1;
			while(strcmp(temp4->file_desc->filename,dirname1)!=0)
			{
				 trav(temp4->first,dirname1,flag,opfile);
				 if(temp4->next==NULL)
				 break;
				temp4=temp4->next;
			}
			if(strcmp(temp4->file_desc->filename,dirname1)==0)
			{
				if(flag==1)
				{
					fprintf(fp,"Printing all directories/files under %s\n",temp4->file_desc->filename);
					//printf("Printing all directories/files under %s\n",temp4->file_desc->filename);
					temp4=temp4->first;
					traverse1(temp4);
				}
				else
				{
					fprintf(fp,"Printing only contents under %s directory\n",temp4->file_desc->filename);
					//printf("Printing all directories/files under %s\n",temp4->file_desc->filename);
					temp2=temp4->first;
					while(temp2!=NULL)
					{
						//printf("%s\n",temp2->file_desc->filename);
						//fwrite(temp2->file_desc->filename,sizeof(char),10,fp);
						fprintf(fp,"%s\n",temp2->file_desc->filename);
						temp2=temp2->next;
					}	
				}
				
			}
			q1=q1->first;
		}

}

void traverse1(ntree *temp5)
{
	ntree *temp6,*q2;
	q2=temp5;
	if(q2!=NULL)
	{
		temp6=q2;
		do
		{
			//printf("%s\n",temp6->file_desc->filename);
                        //fwrite(temp6->file_desc->filename,sizeof(char),10,fp);
			fprintf(fp,"%s\n",temp6->file_desc->filename);
			traverse1(temp6->first);
			temp6=temp6->next;
		}while(temp6!=NULL);
		q2=q2->first;
	}
}
